package messages;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EditServlet extends HttpServlet 
{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/plain; charset=UTF-8");
		String strId = req.getParameter("id");
		int id = Integer.parseInt(strId);
		ArrayList<Message> list = new ArrayList<Message>();
		Connection con = DBManager.getConnection();
		
		try
		{
			String sql = "select id, account, password, message, delete_flag"
					+ " from messages"
					+ " where id = ?";
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				Message m = new Message();
				m.setId(rs.getInt("id"));
				m.setAccount(rs.getString("account"));
				m.setPassword(rs.getString("password"));
				m.setMessage(rs.getString("message"));
				m.setDelete_flag(rs.getString("delete_flag"));
				list.add(m);
			}
			
			req.setAttribute("list", list);
			RequestDispatcher dispatcher = req.getRequestDispatcher("edit.jsp");
			dispatcher.forward(req, resp);
			rs.close();
			ps.close();
			con.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		
		
	}
	
}
